<template>
  <div class="shopcart">
    <div @click="toggleList" class="content">
      <div class="content-left">
        <div class="logo-wrapper">
          <div class="logo" :class="{'highlight':totalCount>0}">
            <span class="icon-shopping_cart" :class="{'highlight':totalCount>0}"></span>
          </div>
          <div class="num" v-show="totalCount>0">{{totalCount}}</div>
        </div>
        <div class="price" :class="{'highlight':totalPrive>0}">¥ {{totalPrive}}</div>
        <div class="desc">另需配送费 ¥ {{deliveryPrice}}元</div>
      </div>
      <div class="content-right" @click.stop.prevent="pay">
        <div class="pay" :class="payClass">{{payDesc}}</div>
      </div>
    </div>
    <div class="ball-container">
      <div  class="ball" transition="drop" v-for="ball in balls" v-show="ball.show">
        <div class="inner inner-hook"></div>
      </div>
    </div>
    <div transition="fold" class="shopcart-list" v-show="listshow">
      <div class="list-header">
        <h1 class="title">购物车</h1>
        <span @click="empty" class="empty">清空</span>
      </div>
      <div v-el:list-content class="list-content">
        <ul>
          <li class="food" v-for="food in selectFoods">
            <span class="name">{{food.name}}</span>
            <div class="price">
              <span>¥{{food.price*food.count}}</span>
            </div>
            <div class="cartcontrol-warpper">
              <cartcoutotrol :food="food"></cartcoutotrol>
            </div>
          </li>
        </ul>
      </div>
    </div>
  </div>
  <div @click="hidderList" transition="fade" class="list-mask" v-show="listshow"></div>
</template>

<script type="text/ecmascript-6">
  import BScroll from 'better-scroll';
  import cartcoutotrol from 'components/cartcoutotrol/cartcoutotrol';

  export default {
    props: {
      // 用于联动 goods.vue
      // 选择的商品信息（数据从父组件中传入）   这里发生变化 对购物车都是会有相应的影响
      selectFoods: {
        type: Array,
        default() {
          return [];
        }
      },
      deliveryPrice: {
        type: Number,
        default: 0
      },
      minPrice: {
        type: Number,
        default: 0
      }
    },
    data() {
      return {
        // 小球的数组
        balls: [
          // 每个小球的状态
          {
            show: false
          },
          {
            show: false
          },
          {
            show: false
          },
          {
            show: false
          },
          {
            show: false
          }
        ],
        // 用来存储 下洛的小球
        dropBalls: [],
        // 默认购物车详情页 处于折叠
        fold: true
      };
    },
    computed: {
      // 商品的总价  依赖于selectFoods
      totalPrive() {
        let total = 0;
        // 遍历 selectfoods 将商品的价钱*商品的个数
         this.selectFoods.forEach((food) => {
           total += food.price * food.count;
         });
         return total;
      },
      // 够买商品的总数
      totalCount() {
        let count = 0;
        this.selectFoods.forEach((food) => {
          count += food.count;
        });
        return count;
      },
      // 商品配送的计算
      payDesc() {
        if (this.totalPrive === 0) {
          return `¥${this.minPrice}元起送`;
        } else if (this.totalPrive < this.minPrice) {
          let diff = this.minPrice - this.totalPrive;
          return `还差¥${diff}元起送`;
        } else {
          return '去结算';
        }
      },
      // 用来判断 结算 时候的样式
      payClass() {
        if (this.totalPrive < this.minPrice) {
          return 'not-enough';
        } else {
          return 'enough';
        }
      },
      // 购物车列表详情折叠或者展开
      listshow() {
        if (!this.totalCount) {
          this.fold = true;
          return false;
        }
        let show = !this.fold;
        if (show) {
          this.$nextTick(() => {
            if (!this.scroll) {
              this.scroll = new BScroll(this.$els.listContent, {
                click: true
              });
            } else {
              this.scroll.refresh();
            }
          });
        }
        return show;
      }
    },
    // 跟父组件 goods.vue中的方法对应
    methods: {
      // 找到 balls中 找到状态为trur的
      drop(el) {
        console.log(el);
        // 从小球中找到一个隐藏的小球，并将它的状态 设置为true
        for (let i = 0; i < this.balls.length; i++) {
          let ball = this.balls[i];
          if (!ball.show) {
            ball.show = true;
            ball.el = el;
            this.dropBalls.push(ball);
            return;
          }
        }
      },
      // 点击购物车打开页面
      toggleList() {
        if (!this.totalCount) {
          return;
        }
        this.fold = !this.fold;
      },
      // 清空购物车
      empty() {
        this.selectFoods.forEach((food) => {
          food.count = 0;
        });
      },
      // 点击购物车上部
      hidderList() {
        this.fold = true;
      },
      // 点击结算
      pay() {
        if (this.totalPrive < this.minPrice) {
          return;
        }
        window.alert(`支付${this.totalPrive}元`);
      }
    },
    // 用来实现小球的动画
    transitions: {
      drop: {
        // 找到 true的小球
        beforeEnter(el) {
          let count = this.balls.length;
          while (count--) {
            let ball = this.balls[count];
            if (ball.show) {
              // 获取到位于设备的 一个位置 x y 分别等于 left top
              let rect = ball.el.getBoundingClientRect();
              let x = rect.left - 32;
              let y = -(window.innerHeight - rect.top - 22);
              // 知道偏移之后 设置它初始的位置
              el.style.display = ''; // 让他显示
              el.style.webkitTransform = `translate3d(0,${y}px,0)`;
              el.style.transform = `translate3d(0,${y}px,0)`;
              let inner = el.getElementsByClassName('inner-hook')[0];
              inner.style.webkitTransform = `translate3d(${x}px,0,0)`;
              inner.style.transform = `translate3d(${x}px,0,0)`;
            }
          }
        },
        enter(el) {
          // 触发浏览器的重绘
          /* eslint-disable no-unused-vars */
          let rf = el.offsetHeight;
          this.$nextTick(() => {
            el.style.webkitTransform = 'translate3d(0,0,0)';
            el.style.transform = 'translate3d(0,0,0)';
            let inner = el.getElementsByClassName('inner-hook')[0];
            inner.style.webkitTransform = 'translate3d(0,0,0)';
            inner.style.transform = 'translate3d(0,0,0)';
          });
        },
        afterEnter(el) {
          // 重置小球 恢复可用的状态
          let ball = this.dropBalls.shift();
          if (ball) {
            ball.show = false;
            el.style.display = 'none';
          }
          console.log(3333);
        }
      }
    },
    components: {
      cartcoutotrol
    }
  };
</script>

<style lang="stylus" rel="stylesheet/stylus">
  @import "../../common/stylus/minxin.styl";
  .shopcart
    position :fixed
    left :0
    bottom :0
    z-index :50
    width: 100%
    height: 48px
    .content
      display: flex
      background :#141d27
      font-size :0
      .content-left
        flex :1
        .logo-wrapper
          display :inline-block
          position :relative
          top: -10px
          margin :0 12px
          padding :6px
          width: 56px
          height: 56px
          box-sizing :border-box
          vertical-align :top
          border-radius :50%
          background :#141d27
          .logo
            width: 100%
            height: 100%
            border-radius :50%
            background :#2B343D
            text-align :center
            &.highlight
              background :#00A0DC
            .icon-shopping_cart
              color :#808589
              font-size :24px
              line-height :44px
              &.highlight
                color :#ffffff
          .num
            position :absolute
            top:0
            right :0
            width: 24px
            height: 19px
            line-height :19px
            text-align :center
            border-radius :16px
            font-size :16px
            font-weight :700
            color :#ffffff
            background :rgb(240,20,20)
            box-shadow : 0 4px 8px 0 rgba(0,0,0,0.4)
        .price
          display :inline-block
          vertical-align :top
          line-height :24px
          margin-top: 12px
          padding-right :
            px
          box-sizing :border-box
          border-right :1px solid rgba(255,255,255,0.1)
          font-size :16px
          font-weight :700
          color :rgba(255,255,255,0.4)
          &.highlight
            color :#ffffff
        .desc
          display :inline-block
          vertical-align :top
          line-height :24px
          margin:12px 0 0 12px
          font-size :14px
          font-weight :700
          color :rgba(255,255,255,0.4)
      .content-right
        flex :0 0 105px
        width :105px
        .pay
          height: 48px
          line-height :48px
          text-align :center
          font-size :14px
          font-weight :700
          color :rgba(255,255,255,0.4)
          &.not-enough
            background :#2B343B
          &.enough
            background :#00b43c
            color :#fff
    .ball-container
      .ball
        position :fixed
        left: 32px
        bottom :22px
        z-index :200
        &.drop-transition
          transition :all 0.4s cubic-bezier(.59,-0.27,.79,.55)
          .inner
            width: 16px
            height: 16px
            border-radius :50%
            background :rgb(0,160,220)
            transition :all 0.4s linear
    .shopcart-list
      position :absolute
      top: 0
      z-index :-1
      width: 100%
      &.fold-transition
        transition :all 0.5s
        transform :translate3d(0,-100%,0)
      &.fold-enter,&.fold-leave
        transform :translate3d(0,0,0)
      .list-header
        height: 40px
        line-height :40px
        padding :0 18px
        background :#f3f5f7
        border-bottom :2px solid rgba(7,17,27,0.1)
        .title
          float :left
          font-size :14px
          color :#1B242D
        .empty
          float :right
          font-size :12px
          color :#01A0DC
      .list-content
        padding :0 18px
        max-height :217px
        background :#ffffff
        overflow :hidden
        .food
          position :relative
          padding :12px 0
          box-sizing :border-box
          border-1px(rgba(7,17,27,0.1))
          .name
            line-height :24px
            font-size :14px
            color :#07111B
          .price
            position :absolute
            right :90px
            bottom :12px
            line-height :24px
            font-weight :700
            font-size :14px
            color :#F01010
          .cartcontrol-warpper
            position :absolute
            right :0
            bottom :6px
  .list-mask
    position :fixed
    top:0
    left:0
    width: 100%
    height: 100%
    z-index :40
    backdrop-filter :blur(10px)
    &.fade-transition
      transition :all 0.5s
      opacity :1
      background :rgba(7,17,27,0.6)
    &.fade-enter, &.fade-leave
      opacity :0
      background :rgba(7,17,27,0)
</style>

